﻿/**
 * GoText Palm: A program that allows to send text messages over the internet.
 *  Copyright (C) 2008 Nicola 'Gawaine' Racco
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Data;
using System.Data.SqlServerCe;
using System.Data.SqlTypes;
using System.Collections.Generic;
using System.Text;

namespace GoTextPalm.Core.Persistence
{
    class DBConfig
    {
        public static String get(String key)
        {
            try
            {
                SqlCeCommand cmd = DB.Instance.NewCommand();
                cmd.CommandText = "SELECT cvalue FROM Config WHERE ckey = @ckey";
                cmd.Parameters.Add("@ckey", key);
                object o = cmd.ExecuteScalar();
                String value = o != null ? Convert.ToString(cmd.ExecuteScalar()) : null;
                cmd.Dispose();
                return value;
            }
            catch (SqlCeException ex)
            {
                throw new DBException(ex);
            }
        }

        public static void set(String key, String value)
        {
            try
            {
                SqlCeCommand cmd = DB.Instance.NewCommand();
                cmd.CommandText = get(key) == null
                    ? "INSERT INTO Config VALUES (@ckey, @cvalue)"
                    : "UPDATE Config SET cvalue = @cvalue WHERE ckey = @ckey";
                cmd.Parameters.Add("@ckey", key);
                cmd.Parameters.Add("@cvalue", value);
                cmd.ExecuteNonQuery();
                cmd.Dispose();
            }
            catch (SqlCeException ex)
            {
                throw new DBException(ex);
            }
        }
    }
}
